package dmsxl.tanxin;

import java.util.Arrays;

/**
 * Author: Zhang Dongwei
 * Date: 2023/8/1 12:34
 */
public class tx18_435 {

    public static void main(String[] args) {
        int[][] intervals = {{1, 2}, {2, 3}, {3, 4}, {1, 3}};
        System.out.println(eraseOverlapIntervals(intervals));
    }

//    自己写的，和之前射气球类似
    public static int eraseOverlapIntervals(int[][] intervals) {
        Arrays.sort(intervals, (a, b) -> Integer.compare(a[0], b[0]));
        int res = 0;
        for (int i = 1; i < intervals.length; i++){
            if (intervals[i][0] >= intervals[i - 1][1]) continue;
            else {
                intervals[i][1] = Math.min(intervals[i - 1][1], intervals[i][1]);
                res ++;
            }
        }
        return res;
    }
}
